﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Front_UserControl_uxDiscussion : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

     setSecureProtocol(false);
    }
    public void setSecureProtocol(bool bSecure)
    {
        string redirectUrl = null;

        // if we want HTTPS and it is currently HTTP
        if (bSecure && !Request.IsSecureConnection)
            redirectUrl = Request.Url.ToString().Replace("http:", "https:");

        else

            // if we want HTTP and it is currently HTTPS
            if (!bSecure && Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("https:", "http:");

        //else

        // in all other cases we don't need to redirect

        // check if we need to redirect, and if so use redirectUrl to do the job
        if (redirectUrl != null)

            Response.Redirect(redirectUrl);

    }
    protected void Page_Prerender(object sender, EventArgs e)
    {
        DealesDataContext DDC = new DealesDataContext();

        lvAllTopics.DataSource = DDC.GetDiscussionByID1(0, 0, 0);
        lvZuponTopics.DataSource = DDC.GetDiscussionByID1(0, 3, 0);

        if (Session["DealID"] != null)
            lvDealTopics.DataSource = DDC.GetDiscussionByID1(0, 1, Convert.ToInt32(Session["DealID"]));
        else
            lvDealTopics.DataSource = DDC.GetDiscussionByID1(0, 1, 0);

        //if (Session["CityID"] != null)
        //    lvCityTopics.DataSource = DDC.GetDiscussionByID1(0, 2, Convert.ToInt32(Session["CityID"]));
        //else
        //    lvCityTopics.DataSource = DDC.GetDiscussionByID1(0, 2, 0);

        lvAllTopics.DataBind();
     //   lvCityTopics.DataBind();
        lvDealTopics.DataBind();
        lvZuponTopics.DataBind();
    }


    protected void lvAllTopics_DataBound(object sender, EventArgs e)
    {
        ListView lvObj = (ListView)sender;
        foreach (ListViewDataItem item in lvObj.Items)
        {
            System.Web.UI.HtmlControls.HtmlInputHidden hiddenDiscussionID
               = (System.Web.UI.HtmlControls.HtmlInputHidden)item.FindControl("hiddenDID");
            System.Web.UI.HtmlControls.HtmlInputHidden hiddenUserID
                = (System.Web.UI.HtmlControls.HtmlInputHidden)item.FindControl("hiddenUserID");
            Label lblUserC = (Label)item.FindControl("lblUserC");
            Image imgLR = (Image)item.FindControl("imgLR");
            Label lblUserLR = (Label)item.FindControl("lblUserLR");
            Label lblTimeLR = (Label)item.FindControl("lblTimeLR");

            Guid userid = new Guid(hiddenUserID.Value);
            MembershipUser userObj = Membership.GetUser(userid);
            lblUserC.Text = userObj.UserName + ". ";

            DealesDataContext DDC = new DealesDataContext();
            System.Data.Linq.ISingleResult<GetLastReplyResult> GLRResult = DDC.GetLastReply(Convert.ToInt32(hiddenDiscussionID.Value));
            foreach (var GLRItem in GLRResult)
            {
                userid = new Guid(GLRItem.UserID);
                userObj = Membership.GetUser(userid);

                imgLR.ImageUrl = "~/GetImage.aspx?UserID=" + GLRItem.UserID;
                lblUserLR.Text = userObj.UserName;
                lblTimeLR.Text = GLRItem.CreatedDateTime;
            }
        }
    }



    protected void btnAllDiscussionTopic_Click(object sender, EventArgs e)
    {
        DealesDataContext DDC = new DealesDataContext();
        TextBox txtDiscussionTopic = (TextBox)LoginViewToStatrtAllDiscussion.FindControl("txtDiscussionTopic");
        DDC.DiscussionMaster_Insert(0, 0, Membership.GetUser().ProviderUserKey.ToString(), txtDiscussionTopic.Text);
        lvAllTopics.DataBind();
        txtDiscussionTopic.Text = "";
    }

    protected void btnDealDiscussionTopic_Click(object sender, EventArgs e)
    {
        DealesDataContext DDC = new DealesDataContext();
        TextBox txtDiscussionTopic = (TextBox)LoginViewToStatrtDealDiscussion.FindControl("txtDealDiscussionTopic");

        if (Session["DealID"] != null)
        {
            DDC.DiscussionMaster_Insert(1, Convert.ToInt32(Session["DealID"]), Membership.GetUser().ProviderUserKey.ToString(), txtDiscussionTopic.Text);
        }
        else
        {
            DDC.DiscussionMaster_Insert(1, 0, Membership.GetUser().ProviderUserKey.ToString(), txtDiscussionTopic.Text);
        }
        lvDealTopics.DataBind();
        lvAllTopics.DataBind();
        txtDiscussionTopic.Text = "";
    }

    //protected void btnCityDiscussionTopic_Click(object sender, EventArgs e)
    //{
    //    DealesDataContext DDC = new DealesDataContext();
    //    TextBox txtDiscussionTopic = (TextBox)LoginViewToStatrtCityDiscussion.FindControl("txtCityDiscussionTopic");
    //    if (Session["CityID"] != null)
    //    {
    //        DDC.DiscussionMaster_Insert(2, Convert.ToInt32(Session["CityID"]), Membership.GetUser().ProviderUserKey.ToString(), txtDiscussionTopic.Text);
    //    }
    //    else
    //    {
    //        DDC.DiscussionMaster_Insert(2, 0, Membership.GetUser().ProviderUserKey.ToString(), txtDiscussionTopic.Text);
    //    }
    // //   lvCityTopics.DataBind();
    //    lvAllTopics.DataBind();
    //    txtDiscussionTopic.Text = "";
    //}

    protected void btnZuponDiscussionTopic_Click(object sender, EventArgs e)
    {
        DealesDataContext DDC = new DealesDataContext();
        TextBox txtDiscussionTopic = (TextBox)LoginViewToStatrtZuponDiscussion.FindControl("txtZuponDiscussionTopic");
        DDC.DiscussionMaster_Insert(3, 0, Membership.GetUser().ProviderUserKey.ToString(), txtDiscussionTopic.Text);
        lvZuponTopics.DataBind();
        lvAllTopics.DataBind();
        txtDiscussionTopic.Text = "";
    }






}
